package com.item.model.consumer;

import com.item.model.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
@RabbitListener(queues = "topic.a")//监听的队列名称 topic.a
public class QueueAReceiver {

    @Autowired
    RedisUtil redisUtil;

    @RabbitHandler
    public void process(Map<String,String> msgMap) {
        String messageId=msgMap.get("messageId");
        String messageData=msgMap.get("messageData");
        String createTime=msgMap.get("createTime");

        //redis存储messageId 防止多次消费
        if(!Objects.isNull(redisUtil.get("messageId")) && messageId.equals(String.valueOf(redisUtil.get("messageId")))) return;
        log.info("topic消费者A 接收messageId:[{}] messageData:[{}] createTime:[{}]",messageId,messageData,createTime);
        redisUtil.set("messageId",messageId);
    }
}
